package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.mapper.UserMapper;
import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.ClientRepository;
import com.comum.javahomework420242025.repository.ContractRepository;
import com.comum.javahomework420242025.repository.PayPhaseRepository;
import com.comum.javahomework420242025.repository.QuoteRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.Phaser;

@RestController
public class ContractController {
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ClientRepository clientRepository;
    @Autowired
    private QuoteRepository quoteRepository;
    @Autowired
    private PayPhaseRepository payPhaseRepository;
    @PostMapping("/addContract")
    public Result<Long> addContract(@RequestBody Contract contract){
        contract.setId(null);
        System.out.println(contract);
        Contract rescontract = contractRepository.save(contract);
        Long contractid = rescontract.getId();
        Optional<Quote> quotedata = quoteRepository.findById(contract.getQuoteid());
        Quote quote = quotedata.get();

        Long clientid = contractRepository.getClientId(contractid);
        Optional<ClientCompany> clientCompanydata = clientRepository.findById(clientid);
        ClientCompany clientCompany = clientCompanydata.get();
        clientCompany.setOwed(clientCompany.getOwed()+quote.getAllprices());
        clientRepository.save(clientCompany);
        return Result.success(contractid);
    }
    @PostMapping("/updateContract")
    public Result updateContract(@RequestBody Contract contract){
        contractRepository.save(contract);
        return Result.success();
    }
    @DeleteMapping("/deleteContract")
    public Result deleteContract(String id){
        List<PayPhase> res = payPhaseRepository.findAllByContractidAndPayPay(id);
        if(res.size()!=0) return Result.error("客户已付款该合同，无法删除！");
        Optional<Contract> contractdata = contractRepository.findById(Long.valueOf(id));
        Contract contract = contractdata.get();
        Optional<Quote> quotedata = quoteRepository.findById(contract.getQuoteid());
        Quote quote = quotedata.get();

        Long clientid = contractRepository.getClientId(Long.valueOf(id));
        Optional<ClientCompany> clientCompanydata = clientRepository.findById(clientid);
        ClientCompany clientCompany = clientCompanydata.get();
        clientCompany.setOwed(clientCompany.getOwed()-quote.getAllprices());
        clientRepository.save(clientCompany);

        contractRepository.deleteById(Long.valueOf(id));
        return Result.success();
    }
    @PostMapping("/getContracts")
    public Result<PageBean<Contract>> getContracts(
            @RequestBody AskList askList, HttpServletRequest req){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        HttpSession session = req.getSession();
        Long userid = (Long) session.getAttribute("userid");
        String department = userMapper.joinAreaGetDepartmentByuserid(userid);
        PageBean<Contract> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Contract> page = contractRepository.getContracts(str,department,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        return Result.success(pb);
    }
    @GetMapping("/getcontractsinclient")
    public Result<List<Contract>>getClientsComesByClientCompanyid(
            String str, String clientcompanyid){
        List<Contract> res = contractRepository.getClientsComesByClientCompanyid(str,clientcompanyid);
        return Result.success(res);
    }
    @GetMapping("/getresetprices")
    public Result<Double> getresetprices(String contractid){
        List<PayPhase> res = payPhaseRepository.findAllByContractid(contractid);
        Double ans = 0.0;
        for(PayPhase p : res){
            ans+=(p.getOughtpay()-p.getPaypay());
        }
        return Result.success(ans);
    }

}
